package org.zerg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zerg.dao.AddressDao;
import org.zerg.dao.UserDao;
import org.zerg.dto.IsValid;
import org.zerg.entity.Address;
import org.zerg.entity.User;
import org.zerg.service.AddressService;

@Service
public class AddressServiceImpl implements AddressService{

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private AddressDao addressDao;
	
	public IsValid insertAddress(Address address, String token) {
		User user = userDao.getTokenByTime(token);
		if(user == null){
			IsValid isValid = new IsValid(false);
			return isValid;
		}else{
		int uid = user.getId();
		Address ad = addressDao.getAddress(uid);
		if(ad==null){
			address.setUserId(user.getId());
			addressDao.insertAddress(address);
			IsValid isValid = new IsValid(true);
			return isValid;
		}else{
			address.setUserId(user.getId());
			addressDao.updataAddress(address);
			IsValid isValid = new IsValid(true);
			return isValid;
		}
		}
	}

	public Address getAddress(String token) {
		User user = userDao.getTokenByTime(token);
		Address address = addressDao.getAddress(user.getId());
		return address;
	}

}
